package com.itheima.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.jwt.JWTUtil;
import com.alibaba.fastjson.JSON;
import com.itheima.common.Com.Employee;
import com.itheima.common.pojo.PageInfo;
import com.itheima.common.vo.EmployeeVo;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.service.EmployeeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 登入
     *
     * @param username 用户名
     * @param password 密码
     * @return 查询到的数据
     */
    @Override
    public EmployeeVo login(String username, String password) {
        //获取mapper层传回来的数据
        Employee employee = employeeMapper.getEmployee(username, password);
        //判断是否为空
        if (employee == null) {
            return null;
        }


        String key="5201314";
        Map<String,Object> map=new HashMap<>();
        map.put("username", employee.getUsername());
        String token = JWTUtil.createToken(map,key.getBytes());

        redisTemplate.opsForValue().set("token"+token, JSON.toJSONString(employee),30, TimeUnit.MINUTES);
        //将数据层的token和服务层生成的token对比
//        employeeMapper.updateToken(token, employee.getId());
        //将token加入到返回的数据容器中
        //new一个对象
        EmployeeVo employeeVo = new EmployeeVo();
        //bean的工具类，复制数据
        BeanUtils.copyProperties(employee, employeeVo);
        employeeVo.setToken(token);
        return employeeVo;

    }

    /**
     * 登出
     *
     * @param token 从数据层拿到的token令牌
     */
    @Override
    public void logout(String token) {
        //获得数据层的token
        Employee byToken = employeeMapper.getByToken(token);
        employeeMapper.updateById(byToken.getId());
    }

    /**
     * 通过id查询
     *
     * @param id 要查询的 id
     * @return 查询到的数据
     */
    @Override
    public EmployeeVo getById(String id) {
        EmployeeVo employee = employeeMapper.getById(id);
        EmployeeVo employeeVo = new EmployeeVo();
        //工具类，两个容器进行复制数据
        BeanUtils.copyProperties(employee, employeeVo);
        return employeeVo;
    }

    @Override
    public PageInfo<Employee> page(String name, Integer page, Integer pageSize) {
        PageInfo<Employee> pageInfo = new PageInfo<Employee>();

        Integer start = (page - 1) * pageSize;
        List<Employee> page1 = employeeMapper.page(name, start, pageSize);
        pageInfo.setRecords(page1);
        pageInfo.setCurrent(start);
        pageInfo.setSize(pageSize);
        long count = employeeMapper.countEmployee(name);
        pageInfo.setTotal(count);
        return pageInfo;

    }

    @Override
    public boolean add(Employee employee) {


        //雪花id
        String snowflakeNextIdStr = IdUtil.getSnowflakeNextIdStr();
        employee.setId(snowflakeNextIdStr);
        employeeMapper.add(employee);
        return true;
    }

    @Override
    public void update(Employee employee) {
        employeeMapper.update(employee);
    }

//    @Override
//    public void getByUsername(String username) {
//        employeeMapper.getByUsername(username);
//
//    }
}
